package org.ChrisRiffle.Acme.BoundingBox.ui;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.GroupLayout.Alignment;

import org.ChrisRiffle.Acme.BoundingBox.controlers.TxtFieldInputVerifier;

/**
 * BoxQuerySwingPanel
 * 
 * This is the main top-level panel that has all the 
 * control swing form components on it 
 * 
 * @author RIF
 * 
 */
public class BoxQueryPanel extends DebugBorderedPanel {
    
	//used for sizing
	private static final int TXT_FIELD_COLUMNS = 6;
	
	
    private JPanel fieldEntryPanel;
    
    private JFormattedTextField leftXTxtField;

    private JFormattedTextField upperYTxtField;

    private JFormattedTextField rightXTxtField;

    private JFormattedTextField lowerYTxtField;
    
    private JButton runQueryButton;
    
    // enforces no negative numbers, and x/y range relative of 
    // each other
    private TxtFieldInputVerifier txtFieldFloatVerifier;
    
    //format mask decimal format for the point txt fields
    private DecimalFormat dF = new DecimalFormat("###.##");
    
    
	/**
	 * Constructor
	 */
	public BoxQueryPanel() {

		layoutThisMainPanel();
	}
    

    
	/**
	 * layoutThisMainPanel
	 */
	private void layoutThisMainPanel() {

		GroupLayout groupLayout = new GroupLayout(this);	
		setLayout(groupLayout);

		groupLayout.setAutoCreateGaps(true);	

		JSeparator seperator = new JSeparator();


		//horizontal gorup
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup()
				.addComponent(getFieldEntryPanel(), Alignment.CENTER)
				.addComponent(seperator)
				.addComponent(getRunQueryButton(), Alignment.TRAILING)

		);

		// vertical group
		groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()	
				.addComponent(getFieldEntryPanel())
				.addGap(4)
				.addComponent(seperator)
				.addComponent(getRunQueryButton())
				.addGap(8)

		);

	}
    
    
    
	/**
	 * Gets the runQueryButton
	 * 
	 * @return the runQueryButton
	 */
	public JButton getRunQueryButton() {
		
		if (runQueryButton == null) {
			
			runQueryButton = new JButton("Run");

		}
		return runQueryButton;
	}
    

    
    /**
     * getFieldEntryPanel
     * 
     * @return
     * 		FieldEntryPanel
     */
    private JPanel getFieldEntryPanel() {
	
	if(fieldEntryPanel == null){
	    
	    fieldEntryPanel = new DebugBorderedPanel();
	    
		GroupLayout groupLayout = new GroupLayout(fieldEntryPanel);
		fieldEntryPanel.setLayout(groupLayout);

		groupLayout.setAutoCreateGaps(true);
		groupLayout.setAutoCreateContainerGaps(true);
		
		JLabel leftXLabel = new JLabel("Left X:");
		JLabel rightXLabel = new JLabel("Right X:");
		JLabel upperYLabel = new JLabel("Upper Y:");
		JLabel lowerYLabel = new JLabel("Lower Y:");
		
		//horizontal group
		groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
								
				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
				  .addComponent(leftXLabel)
				  .addComponent(upperYLabel)				
				)
								
				.addGroup(groupLayout.createParallelGroup()
				  .addComponent(getLeftXTxtField())
				  .addComponent(getUpperYTxtField())				
				)
				
				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
				  .addComponent(rightXLabel)		
				  .addComponent(lowerYLabel)
				)
				
				.addGroup(groupLayout.createParallelGroup()
				  .addComponent(getRightXTxtField())
				  .addComponent(getLowerYTxtField())
				)						
		);
		
		//vertical group
		groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
			
			.addGroup(groupLayout.createParallelGroup(Alignment.CENTER, false)
			  .addComponent(leftXLabel)
			  .addComponent(getLeftXTxtField())
			  .addComponent(rightXLabel)
			  .addComponent(getRightXTxtField())
			)	

			.addGroup(groupLayout.createParallelGroup(Alignment.CENTER, false)
			  .addComponent(upperYLabel)	
			  .addComponent(getUpperYTxtField())
			  .addComponent(lowerYLabel)
			  .addComponent(getLowerYTxtField())
			)			
		);	
		
		// if you link buttons, they will NOt grow/resize when the dialog does
		// but we don't care(for now) b/c we're not allowing the dialog to be resized, 
		// (there's no need)  -- and it could become ugly looking with huge txt fields 
		// stretching across the entire monitor width
		groupLayout.linkSize(getLeftXTxtField(), getUpperYTxtField(), getRightXTxtField(), getLowerYTxtField());
	  }
	
	  return fieldEntryPanel;	    
    }

    
    
    /**
     * gets the TxtFieldFloatVerifier
     * 
     * @return
     * 		the TxtFieldFloatVerifier
     */
    private TxtFieldInputVerifier getTxtFieldFloatVerifier(){;
      if(txtFieldFloatVerifier == null){
    	  txtFieldFloatVerifier = new TxtFieldInputVerifier();
      }
      return txtFieldFloatVerifier;
    }
    
    
	/**
	 * Gets the leftXTxtField
	 * 
	 * @return the leftXTxtField
	 */
    public JFormattedTextField getLeftXTxtField() {
		if (leftXTxtField == null) {
			leftXTxtField = new JFormattedTextField(dF);						
			leftXTxtField.setColumns(TXT_FIELD_COLUMNS);
			leftXTxtField.setForeground(Color.BLUE);
			leftXTxtField.setInputVerifier(getTxtFieldFloatVerifier());
		}
		return leftXTxtField;
	}
    
    

	/**
	 * Gets the upperYTxtField
	 * 
	 * @return the upperYTxtField
	 */
	public JFormattedTextField getUpperYTxtField() {
		if (upperYTxtField == null) {
			upperYTxtField = new JFormattedTextField(dF);
			upperYTxtField.setColumns(TXT_FIELD_COLUMNS);
			upperYTxtField.setForeground(Color.BLUE);
			upperYTxtField.setInputVerifier(getTxtFieldFloatVerifier());
		}
		return upperYTxtField;
	}

    
    
	/**
	 * Gets the rightXTxtField
	 * 
	 * @return the rightXTxtField
	 */
	public JFormattedTextField getRightXTxtField() {
		if (rightXTxtField == null) {
			rightXTxtField = new JFormattedTextField(dF);
			rightXTxtField.setColumns(TXT_FIELD_COLUMNS);
			rightXTxtField.setForeground(Color.BLUE);
			rightXTxtField.setInputVerifier(getTxtFieldFloatVerifier());
		}
		return rightXTxtField;
	}

	
	
	/**
	 * Gets the lowerYTxtField
	 * 
	 * @return the lowerYTxtField
	 */
	public JFormattedTextField getLowerYTxtField() {
		if (lowerYTxtField == null) {
			lowerYTxtField = new JFormattedTextField(dF);
			lowerYTxtField.setColumns(TXT_FIELD_COLUMNS);
			lowerYTxtField.setForeground(Color.BLUE);
			lowerYTxtField.setInputVerifier(getTxtFieldFloatVerifier());
		}
		return lowerYTxtField;
	}
    
        
	/**
	 * Tester
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		JDialog d = new JDialog();
		BoxQueryPanel p = new BoxQueryPanel();
		
		JPanel p2 = p.getFieldEntryPanel();
		
		d.setContentPane(p2);
		d.pack();
		d.setLocationRelativeTo(null);
		d.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {				
				System.exit(0);
			}
		});
		
		d.setVisible(true);
		
	}
}


